package com.pricetag.pricetagandroid;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import com.fedorvlasov.lazylist.LazyAdapter;
import com.pricetag.googleproductsearch.GoogleShopPriceSearchResult;
import com.pricetag.helpers.Constants;
import com.pricetag.helpers.Utils;
import com.pricetag.search.SearchHandler;
import com.pricetag.search.SearchRecord;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

public class GoogleProductSearchResultActivity extends Activity {
	private String searchRecordID;
	private SearchRecord searchRecord;
	private LinearLayout detailContentContainerLayout;
	private Bitmap localImageBitmap;
	private ImageView localImageView;
	private LayoutInflater inflater;
	private LazyAdapter adapter;
	private static int currentColor = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		setContentView(R.layout.activity_google_product_search_result);
 
        Bundle bundle = this.getIntent().getExtras();
		searchRecordID = (String) bundle.get(Constants.SEARCH_RECORD_ID);
		searchRecord = SearchHandler.getSearchRecord(searchRecordID);
		Integer locationSelection = (Integer) bundle.get(Constants.LOCATION_SELECTION);
		
		
		detailContentContainerLayout = (LinearLayout) this.findViewById(R.id.detail_content_container_layout);
		detailContentContainerLayout.removeAllViews();
		detailContentContainerLayout.setBackgroundColor(Color.WHITE);
		
		switch(locationSelection){
		
//		case 0:
//			setupLocalStorePaneLayout();
//			break;
		case 0:
			setupIQEnginePaneLayout();
			break;
		case 1:
			setupGoogleGogglePaneLayout();
		
		}
		
		
		ScrollView scrollView = (ScrollView) this.findViewById(R.id.search_detail_scroll_view);
		scrollView.smoothScrollTo(0, 0);
		scrollView.setBackgroundColor(Color.WHITE);

	}
	
	
	private void setupLocalStorePaneLayout() {

		if (searchRecord.getLocalImageDBSearchResult() == null) {
			return;
		}
		
		currentColor = 1;
		TextView onlineView = this.createFormattedTextView("COMPARE WITH ONLINE STORE PRICE:");
		if (searchRecord.getLocalNameGoogleShopSearchResults() != null) {
			ArrayList<GoogleShopPriceSearchResult> resultsList = searchRecord.getLocalNameGoogleShopSearchResults();
			detailContentContainerLayout.addView(onlineView);
			detailContentContainerLayout.addView(this.createLazylistContainerView(resultsList));
		}

	}
	
	
	private void setupGoogleGogglePaneLayout() {

		if (searchRecord.getGoogleGoggleProductName() == null) {
			return;
		}
		currentColor = 1;
		
		LinearLayout nameBar = new LinearLayout(this);
		nameBar.setOrientation(LinearLayout.HORIZONTAL);
		TextView googleNameView = this.createFormattedTextView(searchRecord.getGoogleGoggleProductName().toUpperCase());
		googleNameView.setMaxWidth(300);
		
		
		LinearLayout iconSet = new LinearLayout(this);
		iconSet.setOrientation(LinearLayout.HORIZONTAL);
		iconSet.setGravity(Gravity.RIGHT);
		iconSet.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT, 0f));
		ImageView upIcon = new ImageView(this);
		upIcon.setLayoutParams(new LinearLayout.LayoutParams(48,48,0f));
		Drawable upDrawable= getResources().getDrawable(R.drawable.arrow_up);
		upIcon.setImageDrawable(upDrawable);
		upIcon.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				adapter.sort(new Comparator<GoogleShopPriceSearchResult>(){

					@Override
					public int compare(GoogleShopPriceSearchResult lhs,
							GoogleShopPriceSearchResult rhs) {
						if(Double.parseDouble(lhs.getPrice())<Double.parseDouble(rhs.getPrice())){
							return -1;
						} else if (Double.parseDouble(lhs.getPrice())>Double.parseDouble(rhs.getPrice())){
							return 1;
						}
						return 0;
					}
				});
				adapter.notifyDataSetChanged();
			}
			
		});
		
		ImageView downIcon = new ImageView(this);
		downIcon.setLayoutParams(new LinearLayout.LayoutParams(48,48,0f));
		Drawable downDrawable= getResources().getDrawable(R.drawable.arrow_down);
		downIcon.setImageDrawable(downDrawable);
		downIcon.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				adapter.sort(new Comparator<GoogleShopPriceSearchResult>(){

					@Override
					public int compare(GoogleShopPriceSearchResult lhs,
							GoogleShopPriceSearchResult rhs) {
						if(Double.parseDouble(lhs.getPrice())<Double.parseDouble(rhs.getPrice())){
							return 1;
						} else if (Double.parseDouble(lhs.getPrice())>Double.parseDouble(rhs.getPrice())){
							return -1;
						}
						return 0;
					}
				});
				adapter.notifyDataSetChanged();
			}
			
		});
		iconSet.addView(upIcon);
		iconSet.addView(downIcon);
		
		nameBar.addView(googleNameView);
		nameBar.addView(iconSet);
		
		
		detailContentContainerLayout.addView(nameBar);
		
		
		
		
		
		

		if (searchRecord.getGoogleNameGoogleShopSearchResults() != null) {
			ArrayList<GoogleShopPriceSearchResult> resultsList = searchRecord.getGoogleNameGoogleShopSearchResults();
			detailContentContainerLayout.addView(this.createLazylistContainerView(resultsList));
		}

	}

	private void setupIQEnginePaneLayout() {

		if (searchRecord.getIqEngineProductName() == null) {
			return;
		}
		currentColor = 1;
		
		LinearLayout nameBar = new LinearLayout(this);
		nameBar.setOrientation(LinearLayout.HORIZONTAL);
		TextView iqEngineNameView = this.createFormattedTextView(searchRecord.getIqEngineProductName().toUpperCase());
		iqEngineNameView.setMaxWidth(300);
		
		LinearLayout iconSet = new LinearLayout(this);
		iconSet.setOrientation(LinearLayout.HORIZONTAL);
		iconSet.setGravity(Gravity.RIGHT);
		iconSet.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT, 0f));
		ImageView upIcon = new ImageView(this);
		upIcon.setLayoutParams(new LinearLayout.LayoutParams(48,48,0f));
		Drawable upDrawable= getResources().getDrawable(R.drawable.arrow_up);
		upIcon.setImageDrawable(upDrawable);
		upIcon.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				adapter.sort(new Comparator<GoogleShopPriceSearchResult>(){

					@Override
					public int compare(GoogleShopPriceSearchResult lhs,
							GoogleShopPriceSearchResult rhs) {
						if(Double.parseDouble(lhs.getPrice())<Double.parseDouble(rhs.getPrice())){
							return -1;
						} else if (Double.parseDouble(lhs.getPrice())>Double.parseDouble(rhs.getPrice())){
							return 1;
						}
						return 0;
					}
				});
				adapter.notifyDataSetChanged();
			}
			
		});
		
		ImageView downIcon = new ImageView(this);
		downIcon.setLayoutParams(new LinearLayout.LayoutParams(48,48,0f));
		Drawable downDrawable= getResources().getDrawable(R.drawable.arrow_down);
		downIcon.setImageDrawable(downDrawable);
		downIcon.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				adapter.sort(new Comparator<GoogleShopPriceSearchResult>(){

					@Override
					public int compare(GoogleShopPriceSearchResult lhs,
							GoogleShopPriceSearchResult rhs) {
						if(Double.parseDouble(lhs.getPrice())<Double.parseDouble(rhs.getPrice())){
							return 1;
						} else if (Double.parseDouble(lhs.getPrice())>Double.parseDouble(rhs.getPrice())){
							return -1;
						}
						return 0;
					}
				});
				adapter.notifyDataSetChanged();
			}
			
		});
		iconSet.addView(upIcon);
		iconSet.addView(downIcon);
		
		nameBar.addView(iqEngineNameView);
		nameBar.addView(iconSet);
		
		
		detailContentContainerLayout.addView(nameBar);
		
		if (searchRecord.getIqEngineNameGoogleShopSearchResults() != null) {
			ArrayList<GoogleShopPriceSearchResult> resultsList = searchRecord.getIqEngineNameGoogleShopSearchResults();
			detailContentContainerLayout.addView(this.createLazylistContainerView(resultsList));
		}

	}

	
	
	private TextView createFormattedTextView(String name) {
		int[] colors = { Color.LTGRAY, Color.WHITE };
		TextView textView = new TextView(this);
		textView.setText(name);
		textView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
		textView.setTextSize(15);
		textView.setBackgroundColor(colors[currentColor]);
		currentColor = 1;
		return textView;
	}
	
	
	private View createLazylistContainerView(ArrayList<GoogleShopPriceSearchResult> resultsList) {
		if (inflater == null) {
			return null;
		}

		List<GoogleShopPriceSearchResult> subList = null;
		if (resultsList.size() >= Constants.LAZY_LIST_DEFAULT_SIZE) {
			subList = resultsList.subList(0, Constants.LAZY_LIST_DEFAULT_SIZE);
		} else {
			subList = resultsList;
		}
		int lazyListHeight = Constants.LAZY_LIST_ITEM_HEIGHT * subList.size() + 68 * subList.size();

		View layoutView = inflater.inflate(R.layout.layout_lazy_list, null);
		layoutView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
				lazyListHeight, 0f));

		ListView list = (ListView) layoutView.findViewById(R.id.list_view_lazy_list);
		adapter = new LazyAdapter(this,android.R.id.text1 ,subList);
		list.setAdapter(adapter);
		list.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, lazyListHeight, 0f));

		return layoutView;
	}
	
	
	private class GetImageBitmapTask extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {
			if (searchRecord.getLocalImageDBSearchResult() != null) {
				localImageBitmap = Utils.getImageBitmap(searchRecord.getLocalImageDBSearchResult().getImage());
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void results) {
			if (localImageView != null) {
				localImageView.setImageBitmap(localImageBitmap);
			}
		}

	}


}


